之前的幾天,介紹了關於 inputs
的使用,有提到 inputs
是 GitLab CI/CD Components 的一部分,因此,在接下來會開始更深入的介紹 GitLab CI/CD 的 Components。
GitLab CI/CD Components 是從 GitLab 16.0 開始進行實驗階段,而後在 GitLab 17.0 正式釋出,Components 除了之前提到的 inputs
用來定義 Component 的輸入規格優點以外,還有一些優點:
首先,這邊要先透過 GitLab 官方提供的 Component Template 來建立第一個 CI/CD Component:
完成 CI/CD Component 專案的建立
在建立專案中,可以看到這個 Template 中已經包含了一個 templates 資料夾及其元件與相關 .gitlab-ci.yml
檔案。
── templates/
└── my-component.yml
── LICENSE.md
── README.md
── .gitlab-ci.yml
``
這是一個已經可以使用的 Component 專案,接著只要為專案建立版本 Git Tag,就可以提供給其他使用者使用(沒有上 Tag 也可以透過 Git SHA 使用)。這邊我們為目前的主分支建立一個 Tag 1.0.0。建立完畢後,會觸發一個釋出版本的 Pipeline,這時候其他專案就可以以版本號 1.0.0 使用這個專案的 Component。
有了可以使用的 Component,接著試著使用這個 CI/CD Component,在另外的專案中,編輯 .gitlab-ci.yml
,可以使用以下的範例:
include:
- component: $CI_SERVER_FQDN/mo-playground/gitlab-ci-example-2025/hello-component-2025/my-component@384cbe6028c44ae286c3d3a9e2f71276c6132510
inputs:
stage: build
- component: $CI_SERVER_FQDN/mo-playground/gitlab-ci-example-2025/hello-component-2025/my-component@1.0.0
inputs:
stage: build
job_name: job_from_version
在完成編輯後,換看到觸發了一個 pipeline ,這個 Pipeline 中會有兩個 Job,分別是 job-template
及 job_from_version
,其執行的結果會是:
$ echo "Starting job job-template"
Starting job job-template
及
$ echo "Starting job job_from_version"
Starting job job_from_version
從範例中 include
的內容中有兩個 component
,其對應的數值為 Component 的路徑,Component 的專案路徑格式為:
<fully-qualified-domain-name>/<project-path>/<component-name>@<specific-version>
$CI_SERVER_FQDN
。mo-playground/gitlab-ci-example-2025/hello-component-2025
,查看路徑最快的方法就是看目前 GitLab 的網址上連結。my-component.yml
則這個檔案的檔案名稱,即為 component-name。384cbe6028c44ae286c3d3a9e2f71276c6132510
,也可以如第二個引入的 component 使用的方法,直接使用版本號 1.0.0
這個 Tag。Hello Component 2025
專案:這個專案是由 GitLab 官方提供的 Component Template 直接 Import 而來,是一個已經包含 Component 的基本樣貌,如基礎元件 my-component.yml
並且在 .gitlab-ci.yml
中也可以看到對於這個元件的測試、版本釋出等工作。
一個 Component 專案,必須要包含:
README.md
:Markdown 格式的專案說明檔templates/
資料夾,裡頭用來置放 CI/CD Component。my-component.yml
,也可以是一個資料夾的形式,例如資料夾名稱 my-other-component
,以資料夾的形式建立 Component,則其資料夾內必須要有一個 template.yml
用來撰寫該 component。.gitlab-ci.yml
用來做 Component 的 CI/CD,如 Component 測試或版本釋出。LICENSE.md
用來宣告這個 Component 的授權模式。在今天的案例中,初探 GitLab CI/CD Component 的樣貌,包含了建立第一個 Component,還有在另外的專案中使用這個剛建立好的 Component,也稍微描述了可能可以怎麼使用,還有一個 Component 基礎需要包含的檔案及結構初探。接下來的內容,我們會更深入來了解 GitLab CI/CD Component。我是墨嗓(陳佑竹),期待這次的內容能帶給你實用的啟發與幫助。